Hello World in Lua

What's Lua? "Lua is a powerful light-weight programming language designed for extending applications" : http://www.lua.org/

And with the beta release of a new plugin from Flying Meat, you can write simple lua scripts to extend VoodooPad.

To get started, you'll want to download and install this VoodooPad plugin:
http://flyingmeat.com/download/plugin/LuaPluginEnabler.vpplugin.zip

Simply download, unzip, and double click the file and VoodooPad will install it for you. I should also mention that because this is a beta release, you'll be doing this again in the future and possibly multiple times at that. So if the word beta (aka, dangerous!) gives you the willies, you might not want to continue. But if you are adventurous and make regular backups of your VoodooPad documents then continue right along.

Once the plugin is in place you'll want to quit and restart VoodooPad so that it registers the lua plugin. Now when you start VoodooPad you should notice some new menus under the "Plugin" menu like "Lua" and "Word Count".

lua plugin menu items

Go ahead and try the one named "In Page". This will tell you the number of words, lines, and characters in the current VoodooPad page. It's pretty simple, and this is an example of a plugin that's written in lua.

Now we're going to create our own plugin- a really simple one that just inserts the words "Hello World" into the current page. I promise it'll be really easy.

First, create a new page in your VoodooPad document named "Lua Test". When the page comes up, select everything and delete it. We're going to want a completely empty page to write this in.

Next, type (or cut and paste) the following line into your new page:

windowController:textView():insertText("Hello World")

Now go to the Plugin->Lua->Run Page as Lua Plugin menu item. You should then see the text "Hello World" inserted into the page. Congratulations! That's all it takes to make a simple VoodooPad plugin. Now let's explain what's going on.

There is a variable in every lua script plugin we write named "windowController". This can be thought of as a reference to the window that the plugin is running in. We can then call various methods on the windowController, one such is textView(). This returns the editing portion of the window- the one where we actually do the typing in VoodooPad. We could have also written this plugin as:

textView = windowController:textView()
textView:insertText("Hello World")

But that's two lines and isn't as good of a demo. But it is however a little bit clearer.

So windowController is a reference to window stuff, and textView is a reference to the editing text part of the window. Then when we call the method insertText(), passing in the string Hello World, we're saying insert the text "Hello World", in the text view of the current window.

That's all great- but how do we get our Hello World plugin up in the menu bar?

First, let's open up the TextEdit application. Create a new document and paste the script we wrote in there. Next, go to the Format menu and choose "Make Plain Text".

Lua script in TextEdit

Now, we're going to save our document to the folder "~/Library/Application Support/VoodooPad/Script PlugIns". Let's call it "Hello World.lua". Make sure it's ends with ".lua" and not ".txt". And make sure to uncheck the box that says "If no extension is provided, use ".txt" in the save dialog box.

Lua script save

Now quit and restart VoodooPad. You should now see the menu item "Hello World" under the Plugin->Lua menu.

Lua script save

Tada! That's it. Go head and choose it, and you should see the text "Hello World" insert into the current page.